<?php

namespace app\admin\controller;

use think\Request;

use app\admin\model\SendOrderModel;

class Sendorder extends Base
{
    //列表
    public function index()
    {
        if(request()->isAjax()){

            $param = input('param.');

            $limit = $param['pageSize'];
            $offset = ($param['pageNumber'] - 1) * $limit;

            $where = [];
            if (!empty($param['searchText'])) {
                $where[] = ['send_code', 'like', '%' . $param['searchText'] . '%'];
            }
            $user = new SendOrderModel();
            $selectResult = $user->getUsersByWhere($where, $offset, $limit);

            // 拼装参数
            foreach($selectResult as $key=>$vo){
                if(!empty($vo['sender_id'])){
                    $selectResult[$key]['sendphone'] = showOperate([
                        $vo->sender => [
                            'auth' => 'member/edit',
                            'href' => "javascript:memberInfo(" .$vo['sender_id'] .")",
                            'btnStyle' => 'primary',
                            'icon' => 'fa fa-address-book'
                        ]
                    ]);
                }
                $selectResult[$key]['uphone'] = showOperate([
                    $vo->memberModel()->value('memb_phone') => [
                        'auth' => 'member/edit',
                        'href' => "javascript:memberInfo(" .$vo['send_uid'] .")",
                        'btnStyle' => 'primary',
                        'icon' => 'fa fa-address-book'
                    ]
                ]);

                $selectResult[$key]['operate'] = showOperate($this->makeButton($vo['id']));
            }
            $return['total'] = $user->getAllUsers($where);  //总数据
            $return['rows'] = $selectResult;
            return json($return);
        }

        return $this->fetch();
    }

    // 编辑
    public function edit()
    {
        $user = new SendOrderModel();

        if(request()->isPost()){

            $param = input('post.');
            
            $flag = $user->editUser($param);

            return json(msg($flag['code'], $flag['data'], $flag['msg']));
        }

        $id = input('param.id');
        $sendo = SendOrderModel::find($id);
        $sendo->append(['memberModel'=>'name']);
        return $this->fetch('edit', ['sendorder'=> $sendo]);
    }

    // 删除用户
    public function reciver()
    {
        $id = input('param.id');

        $role = new SendOrderModel();
        $flag = $role->ownReciver($id);
        return json($flag);
    }

    //查看用户信息
    public function info(){
        $id = input('id');
        $this->assign('member', model('MemberModel')->get($id));
        return view();
    }

    /**
     * 拼装操作按钮
     * @param $id
     * @return array
     */
    private function makeButton($id)
    {
        $button = [
            '编辑' => [
                'auth' => 'sendorder/edit',
                'href' => url('sendorder/edit', ['id' => $id]),
                'btnStyle' => 'primary',
                'icon' => 'fa fa-paste'
            ]
        ];
        // if(db('send_order')->where('id', $id)->value('send_status') == 0){
        //     $button['商家接单'] = [
        //         'auth' => 'sendorder/reciver',
        //         'href' => "javascript:reciver(" .$id .")",
        //         'btnStyle' => 'danger',
        //         'icon' => 'fa fa-trash-o'
        //     ];
        // }
        return $button;
    }
}
